package com.db.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.DefaultComboBoxModel;

import org.apache.commons.io.FileUtils;

import com.base.MyComModel;
import com.data.Data;
import com.db.bean.Workclass;
import com.db.dao.WorkClassDAOImpl;
/**
* @author 作者:秋哲    E-mail:2810201146@qq.com:
* @version 创建时间：2020年5月22日 上午7:25:09
* @description: 
*/
public class GroupServer {
	//获取组列表 
	public void initGroupList(){
		int user_id = Data.UserBean.getId();
		List<Workclass> groupList = new WorkClassDAOImpl().getAll(user_id);
		Data.workgroup = new HashMap<String, Integer>();
		for (Workclass workclass : groupList) {
			Data.workgroup.put(workclass.getTitle(), workclass.getId());
		}
		GroupToDri();
	}
	//生成目录
	public void GroupToDri() {
		Map<String, Integer> map = Data.workgroup;
		File file1 = new File("data");
		for (String key : map.keySet()) {
			new File(file1,key).mkdirs();
		}
	}
	//把Data静态数据转换为控件支持的格式
	public DefaultComboBoxModel<String> updateDataGroupList(){ 
		Map<String, Integer> map = Data.workgroup;
		List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
		    public int compare(Map.Entry<String, Integer> mapping1, Map.Entry<String, Integer> mapping2) {
		        return mapping1.getValue().compareTo(mapping2.getValue());
		    }
		});
		MyComModel dataByGui = new MyComModel();
		for (Entry<String, Integer> entry : list) {
			dataByGui.myAdd(entry.getKey());
		}
		return dataByGui;
	}
	//新建组
	public int insertWorkGroup(String title){
		if(title.length()==0||title==null||title=="")return 0;
		Integer integer = Data.workgroup.get(title);
		if(integer!=null) return 0;		
		int userID_id = Data.UserBean.getId();
		Workclass worlclass_obj = new Workclass(1, title, userID_id);
		int insert = new WorkClassDAOImpl().insert(worlclass_obj);
		if(insert==1) {
			Workclass workclassDaoObj = new WorkClassDAOImpl().getByTitle(userID_id, title);
			Data.workgroup.put(title, workclassDaoObj.getId());
		}
		return insert;
	}

	//删除组  
	public int deleteWorkGroup(String GUIComText){
		Integer workClass_id = Data.workgroup.get(GUIComText);
		if(workClass_id==null) return 0;
		int n = new WorkClassDAOImpl().deleteById(workClass_id);
		if(n==1) Data.workgroup.remove(GUIComText);
		return n;
	}

	//修改组
	public int updateWorlGroup(String GUIComText,String GUIComNewText){
		int userID_id = Data.UserBean.getId();
		Integer workClass_id = Data.workgroup.get(GUIComText);
		if(workClass_id==null)return 0;
		Integer NewTextById = Data.workgroup.get(GUIComNewText);
		if(NewTextById!=null)return 0;
		Workclass worlclass_obj = new Workclass(workClass_id, GUIComNewText, userID_id);
		int n = new WorkClassDAOImpl().updateWorkClass(worlclass_obj);
		if(n==1) {
			Integer integer = Data.workgroup.get(GUIComText);
			Data.workgroup.remove(GUIComText);
			Data.workgroup.put(GUIComNewText, integer);
		}
		return n;
	}

}
